package MySingleLinkedList;

public class SingleLinkedList {
    class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val=val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1=new ListNode(10);
        ListNode node2=new ListNode(20);
        ListNode node3=new ListNode(30);
        ListNode node4=new ListNode(40);
        ListNode node5=new ListNode(50);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=null;
        head=node1;
    }

    public int size() {
        int count=0;
        ListNode cur=head;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    public void addAtHead(int val) {
        ListNode node=new ListNode(val);
        node.next=head;
        head=node;
    }

    public void addAtTail(int val) {
        ListNode node=new ListNode(val);
        if(head==null) {
            addAtHead(val);
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            cur=cur.next;
        }
        cur.next=node;
    }

    public void addAtIndex(int index,int val) {
        try {
            checkIndexOfAdd(index);
        } catch(IllegalIndexException e){
            e.printStackTrace();
        }
        if(index==0) {
            addAtHead(val);
            return;
        }
        if(index==size()) {
            addAtTail(val);
            return;
        }
        ListNode node=new ListNode(val);
        ListNode cur=head;
        int count=0;    //从0到index-1,走过了(index-1)个节点
        while(count!=(index-1)) {
            cur=cur.next;
            count++;
        }
        node.next=cur.next;
        cur.next=node;
    }

    private void checkIndexOfAdd(int index) throws IllegalIndexException {
        if(index<0 || index>size()) {
            throw new IllegalIndexException("Add时位置不合法!");
        }
    }

    public boolean contains(int val) {
        ListNode cur=head;
        while(cur!=null) {
            if(cur.val==val) {
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //删除第一次出现的val
    public void remove(int val) {
        if(head==null)return;
        if(head.val==val) {
            head=head.next;
            return;
        }
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.next.val==val) {
                cur.next=cur.next.next;
                return;
            }
            cur=cur.next;
        }
    }

    public void removeAllKey(int val) {
        if(head==null) return;
        ListNode cur=head;
        while(cur.next!=null) {
            if(cur.next.val==val) {
                cur.next=cur.next.next;
            } else {
                cur=cur.next;
            }
        }
        if(head.val==val) {
            head=head.next;
        }
    }

    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }

    public void display() {
        ListNode cur=head;
        while(cur!=null) {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
